package demo06235;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class EmployeeManagementSystem {
        private List<Employee> employees = new ArrayList<>();
        private Scanner scanner = new Scanner(System.in);

        public static void main(String[] args) {
            EmployeeManagementSystem system = new EmployeeManagementSystem();
            system.start();
        }

        // 启动系统
        public void start() {
            while (true) {
                showMenu();
                int choice = getInputChoice();
                handleChoice(choice);
            }
        }

        // 显示菜单
        private void showMenu() {
            System.out.println("\n=== 员工管理系统 ===");
            System.out.println("1. 添加员工");
            System.out.println("2. 修改员工");
            System.out.println("3. 删除员工");
            System.out.println("4. 查询员工");
            System.out.println("5. 显示所有员工");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作(0-5): ");
        }

        // 获取用户输入
        private int getInputChoice() {
            while (!scanner.hasNextInt()) {
                System.out.println("请输入数字！");
                scanner.next();
                System.out.print("请重新选择(0-5): ");
            }
            return scanner.nextInt();
        }

        // 处理用户选择
        private void handleChoice(int choice) {
            switch (choice) {
                case 1: addEmployee(); break;
                case 2: updateEmployee(); break;
                case 3: deleteEmployee(); break;
                case 4: queryEmployee(); break;
                case 5: showAllEmployees(); break;
                case 0: exitSystem(); break;
                default: System.out.println("无效的选择！");
            }
        }

        // 添加员工
        private void addEmployee() {
            scanner.nextLine(); // 清除缓冲区
            System.out.println("\n--- 添加员工 ---");

            System.out.print("请输入员工编号: ");
            String id = scanner.nextLine();

            System.out.print("请输入员工姓名: ");
            String name = scanner.nextLine();

            System.out.print("请输入所在部门: ");
            String dept = scanner.nextLine();

            System.out.print("请输入员工薪资: ");
            double salary = scanner.nextDouble();

            employees.add(new Employee(id, name, dept, salary));
            System.out.println("员工添加成功！");
        }

        // 修改员工
        private void updateEmployee() {
            scanner.nextLine(); // 清除缓冲区
            System.out.println("\n--- 修改员工 ---");

            System.out.print("请输入要修改的员工编号: ");
            String id = scanner.nextLine();

            Employee emp = findEmployeeById(id);
            if (emp == null) {
                System.out.println("未找到该员工！");
                return;
            }

            System.out.println("当前员工信息: " + emp);
            System.out.print("请输入新姓名(直接回车跳过): ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) emp.setName(name);

            System.out.print("请输入新部门(直接回车跳过): ");
            String dept = scanner.nextLine();
            if (!dept.isEmpty()) emp.setDept(dept);

            System.out.print("请输入新薪资(输入0跳过): ");
            double salary = scanner.nextDouble();
            if (salary != 0) emp.setSalary(salary);

            System.out.println("员工信息修改成功！");
        }

        // 删除员工
        private void deleteEmployee() {
            scanner.nextLine(); // 清除缓冲区
            System.out.println("\n--- 删除员工 ---");

            System.out.print("请输入要删除的员工编号: ");
            String id = scanner.nextLine();

            Employee emp = findEmployeeById(id);
            if (emp == null) {
                System.out.println("未找到该员工！");
                return;
            }

            employees.remove(emp);
            System.out.println("员工删除成功！");
        }

        // 查询员工
        private void queryEmployee() {
            scanner.nextLine(); // 清除缓冲区
            System.out.println("\n--- 查询员工 ---");

            System.out.print("请输入要查询的员工编号: ");
            String id = scanner.nextLine();

            Employee emp = findEmployeeById(id);
            if (emp == null) {
                System.out.println("未找到该员工！");
            } else {
                System.out.println("查询结果: " + emp);
            }
        }

        // 显示所有员工
        private void showAllEmployees() {
            System.out.println("\n--- 所有员工信息 ---");
            if (employees.isEmpty()) {
                System.out.println("暂无员工信息！");
                return;
            }

            for (Employee emp : employees) {
                System.out.println(emp);
            }
            System.out.println("共 " + employees.size() + " 名员工");
        }

        // 退出系统
        private void exitSystem() {
            System.out.println("感谢使用员工管理系统，再见！");
            scanner.close();
            System.exit(0);
        }

        // 根据ID查找员工
        private Employee findEmployeeById(String id) {
            for (Employee emp : employees) {
                if (emp.getId().equals(id)) {
                    return emp;
                }
            }
            return null;
        }
    }

